package com.maz.androidprojectcomponenttest;

import com.maz.androidprojectcomponenttest.anim.ViewAnimationListener;
import com.maz.androidprojectcomponenttest.fragment.FacebookFragment;
import com.maz.androidprojectcomponenttest.fragment.TestDialog;
import com.maz.androidprojectcomponenttest.modComponents.ModifiedTextView;

import android.os.Bundle;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ComponentTest extends FragmentActivity implements OnClickListener{
	
	private final static String FACEBOOK_FRAGMENT = "Facebook_Fragment";
	private AnimationDrawable animationDrawable;
	private ImageView imageView;
	private FragmentManager fragmentManager;
	private LinearLayout layoutPlaceholder;
	private FrameLayout fragmentContainer;
	
	private final static String DIALOG_ID = "TESTDIALOG";
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.component_test, menu);
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_component_test);
		initLayout();
		initComponent();
	}
	
	private void initLayout(){
		imageView = (ImageView) findViewById(R.id.animationDisplayer);
		layoutPlaceholder = (LinearLayout) findViewById(R.id.additionLayout);
		fragmentContainer = (FrameLayout) findViewById(R.id.fragmentContainer);
		
	}
	
	private void initComponent(){
		imageView.setOnClickListener(this);
		testFacebook();
	}
	
	private void testModTextView(){
		ModifiedTextView modTextView = new ModifiedTextView(this);
		setContentView(modTextView);
	}
	
	private void testDrawableAnimation(){
		
		imageView.setVisibility(View.VISIBLE);
		animationDrawable = (AnimationDrawable) imageView.getDrawable();
	}	
	
	private void testViewAnimation(){
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.sample_view_animation);
		animation.setFillAfter(true);
		animation.setAnimationListener(new ViewAnimationListener());	
		imageView.startAnimation(animation);
	}
	
	private void testPropertyAnimation(){
		/*ObjectAnimator goneAnimation = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
		ObjectAnimator appearAnimation = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);
		ObjectAnimator moveAnimation = ObjectAnimator.ofFloat(imageView, "x", 0f, 200f);
		AnimatorSet set = new AnimatorSet();
		set.play(moveAnimation).before(goneAnimation).before(appearAnimation);
		set.setDuration(5000);
		set.start();*/
		/*AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.sample_property_animation);
		set.setTarget(imageView);
		set.start();*/
		
	}
	
	private void testLayoutAnimation(){
		layoutPlaceholder.addView(buildLayout());
	}
	
	private void testFacebook(){
		FacebookFragment facebookFragment;
		facebookFragment = (FacebookFragment) getSupportFragmentManager().findFragmentByTag(FACEBOOK_FRAGMENT);
		if(facebookFragment == null){
			facebookFragment = new FacebookFragment();
		}
		getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, facebookFragment, FACEBOOK_FRAGMENT).commit();
	}
	
	private RelativeLayout buildLayout(){
		RelativeLayout relativeLayout = new RelativeLayout(this);
		relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		TextView textView = new TextView(this);
		textView.setText("TestingTEXT");
		relativeLayout.addView(textView);
		return relativeLayout;
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		/*
		 * If want to start animation automatically, it must be called in onWindowFocusChanged
		 */
		if(animationDrawable != null){
			animationDrawable.start();
		}
		
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.animationDisplayer){
			/*if(fragmentManager.findFragmentByTag(DIALOG_ID)==null){
				TestDialog testDialog = new TestDialog();
				testDialog.show(fragmentManager, DIALOG_ID);
			}*/
			testLayoutAnimation();
		}
	}

}
